package com.xxx.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xxx.common.Const;
import com.xxx.common.IdWorkerManage;
import com.xxx.common.dto.Menu;
import com.xxx.common.dto.Page;
import com.xxx.common.dto.Pagination;
import com.xxx.dao.SysMenuDao;
import com.xxx.model.SysMenu;
import com.xxx.service.SysMenuService;
import com.xxx.service.SysRoleMenuService;

@Service
public class SysMenuServiceImpl implements SysMenuService {
	
	@Autowired
	private SysMenuDao sysMenuDao;
	
	@Autowired
	private SysRoleMenuService sysRoleMenuService;
	
	@Override
	@Transactional
	public long saveOrUpdate(SysMenu sysMenu) throws Exception {
		if(sysMenu.getId() == null){
			sysMenu.setId(IdWorkerManage.getId());
			return sysMenuDao.save(sysMenu);
		}
		return sysMenuDao.update(sysMenu);
	}

	@Override
	@Transactional
	public boolean delete(long id) throws Exception{
		return sysMenuDao.delete(id);
	}

	@Override
	public SysMenu getEntity(long id) throws Exception{
		return sysMenuDao.getEntity(id);
	}

	@Override
	public int getCount(Map<String, Object> whereMap) {
		return sysMenuDao.getCount(whereMap);
	}
	
	@Override
	public List<SysMenu> getList(Map<String, Object> whereMap) {
		return sysMenuDao.getList(whereMap);
	}

	@Override
	public List<SysMenu> getList(Map<String, Object> whereMap, Pagination pagination) {
		return sysMenuDao.getList(whereMap, pagination);
	}
	
	@Override
	public Page<SysMenu> getPage(Map<String, Object> whereMap, Pagination pagination) {
		int count = this.getCount(whereMap);
		if(count > 0){
			List<SysMenu> list = this.getList(whereMap, pagination);
			return new Page<SysMenu>(pagination.getCurrentPage(), pagination.getPageSize(), count, list);
		}
		return new Page<SysMenu>(pagination.getCurrentPage(), count);
	}

	@Override
	public List<Menu> getMenuList(Map<String, Object> whereMap,long userid) {
		Map<Long, List<SysMenu>> menuMap = this.getParentListMap(whereMap,userid);
		return getMenuList(Const.DEFAULT_LONG_VALUE, menuMap);
	}
	
	/**
	 * 递归获取子菜单
	 * @param menuid
	 * @param map
	 * @return
	 */
	private List<Menu> getMenuList(long menuid,Map<Long, List<SysMenu>> menuMap){
		if(menuMap.containsKey(menuid)){
			List<Menu> menulist = new ArrayList<>();
			Menu menu = null;
			List<SysMenu> list = menuMap.get(menuid);
			for (SysMenu sysMenu : list) {
				menu = new Menu(sysMenu.getId(), sysMenu.getMenuname(), sysMenu.getMenuurl(),sysMenu.getParentid(), sysMenu.getIcon());
				menu.setHasSub(menuMap.containsKey(sysMenu.getId()));
				if(menu.isHasSub()){
					menu.setSubList(getMenuList(sysMenu.getId(), menuMap));
				}
				menulist.add(menu);
			}
			return menulist;
		}
		return null;
	}
	
	/**
	 * Map<parentid,List<SysMenu>>
	 * @param whereMap
	 * @param userid 用户 id 判断权限
	 * @return
	 */
	private Map<Long, List<SysMenu>> getParentListMap(Map<String, Object> whereMap,long userid){
		Set<Long> userRoleMenu = sysRoleMenuService.getUserRoleMenu(userid);
		Map<Long, List<SysMenu>> map = new HashMap<>();
		List<SysMenu> meunList = this.getList(whereMap, new Pagination(1, Integer.MAX_VALUE, "orderno asc"));
		List<SysMenu> list = null;
		for (SysMenu sysMenu : meunList) {
			if(userRoleMenu.contains(sysMenu.getId())){
				long parentid = sysMenu.getParentid();
				list = map.containsKey(parentid) ? map.get(parentid) : new ArrayList<SysMenu>();
				list.add(sysMenu);
				map.put(parentid, list);
			}
		}
		return map;
	}
}
